﻿using SQ.BLL.Factory;
using SQ.BLL.Interfaces;
using SQ.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace SQ.API.Controllers
{
    [RoutePrefix("api/fila")]
    public class FilaController : ApiController
    {
        private IFilaBS userBS = BLLFactory.getFilaBS();

        [Route("ListarFilasPorEstabelecimento/{codigoEstabelecimento}")]
        [HttpGet]
        public HttpResponseMessage ListarFilasPorEstabelecimento(int codigoEstabelecimento)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, userBS.ListarFilasPorEstabelecimento(codigoEstabelecimento));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("ObterFilaAberta/{codigoEstabelecimento}")]
        [HttpGet]
        public HttpResponseMessage ObterFilaAberta(int codigoEstabelecimento)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, userBS.ObterFilaAberta(codigoEstabelecimento));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("ObterFilaPorUsuario/{codigoUsuario}")]
        [HttpGet]
        public HttpResponseMessage ObterFilaPorUsuario(int codigoUsuario)
        {
            try
            {
                return Request.CreateResponse(HttpStatusCode.OK, userBS.ObterFilaPorUsuario(codigoUsuario));
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }

        [Route("AdicionarUsuarioFila")]
        [HttpPost]
        public HttpResponseMessage AdicionarUsuarioFila(IntegranteFila integrante)
        {
            try
            {
                userBS.AdicionarIntegranteFila(integrante);
                return Request.CreateResponse(HttpStatusCode.OK, integrante);
            }
            catch (ArgumentException argEx)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest, argEx.Message);
            }
            catch (Exception ex)
            {
                return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
            }
        }
    }
}